const value = req.query.q;
// const appid = "xxxxx";
// const key = "xxxx"

const axios = require('axios')
const MD5 = require('md5');
 
function convertToSearchParams(obj) {
    const params = [];
    for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
            const encodedKey = encodeURIComponent(key);
            const encodedValue = encodeURIComponent(obj[key]);
            const param = `${encodedKey}=${encodedValue}`;
            params.push(param);
        }
    }
    return params.join('&');
}

const appid = 'xxxx';
const key = 'xxxx';

const salt = new Date();
const sign = MD5(appid + value + salt + key);
axios.get(`http://api.fanyi.baidu.com/api/trans/vip/translate?${convertToSearchParams({ ...req.query, salt, sign, appid })}`).then(_res => {
    // console.log('ss', _res.data)
    let text = ""
    try {
        text = _res?.data?.trans_result.pop()?.dst
    } catch (e) {
        text = value;
    }
    res.send({
        status: 200, data: text, msg: null
    })
}).catch(e => {
    // console.log(e, '钱钱钱')
    res.send('error info:' + e.message);
    res.send({ msg: e.message, status: 500, data: e.message })
})
